package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartDestroyBinding
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/23
 *描述: 配件毁型拍照 页面
 */
class PartDestroyActivity : BaseActivityV2() {
   private val mBinding by lazy { ActivityPartDestroyBinding.inflate(layoutInflater) }
   override fun getLayout() = mBinding.root
   private val imageShowList = ArrayList<String>()
   private val imageResultCode = 100
   private val orderId by lazy { intent.getIntExtra(MyConstants.ID, -1) }
   private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }

   override fun event() {
      mBinding.mPhotoNGV.init(this, imageShowList, 6, imageResultCode)

   }
   override fun click() {
      mBinding.mConfirmBTN.setOnClickListener {
         if (imageShowList.isEmpty()) {
            showToast("请至少选择一张照片上传")
         } else {
            val fileList = ArrayList<File>()
            var index=0
            imageShowList.forEach {
               index++
               val file = File(it)
               //图片压缩
//                    val newFile: File = CompressHelper.getDefault(this).compressToFile(file)

               val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
//               val newFile= ImageUtil.imageWaterMark(this,file,index)
               fileList.add(newFile)
            }
            upDataFile(fileList) {
               val dataList = ArrayList<String>()
               it?.forEach { b ->
                  dataList.add(b.fileUrl.handlerNull())
               }
               val map=HashMap<String,Any>()
               map["orderId"]=orderId
               map["picUrls"]=dataList

               ApiMethods.apiSubscribe(ApiMethods.retrofit.partHandlerSubmitV2(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                  showToast("提交成功")
                  EventBus.getDefault().post(PositionEvent(mListPosition))
                  finish()
               })

//               commonDialog("确认拆解么") {
//                  ApiMethods.apiSubscribe(ApiMethods.retrofit.partHandlerSubmit(
//                     mList[position].orderId
//                  ), MyObserverHandlerLoading(this, loading) {
//                     MyLog.d("回调:$it")
//                     showToast("操作成功")
//                     page = 1
//                     getData()
//                  })
//               }
            }
         }
      }
   }

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      data?.let {
         when (requestCode) {
            //图片回调
            imageResultCode -> {
               val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
               val resultList: MutableList<NineGridBean> = ArrayList()
               list?.forEach { imageBean ->
                  val nineGirdData = NineGridBean(imageBean.imagePath)
                  imageShowList.add(imageBean.imagePath)
                  resultList.add(nineGirdData)
               }
               mBinding.mPhotoNGV.addDataList(resultList)
            }
            else -> {

            }
         }
      }
   }
}